package TicketSaleSimulation;

public class TicketSaleSimulation {
    // 总共可售的机票数量
    private static final int TOTAL_TICKETS = 138;
    // 售票窗口数量
    private static final int WINDOW_COUNT = 5;
    // 已售出的票数
    private static int soldTickets = 0;

    public static void main(String[] args) {
        // 创建并启动5个售票窗口线程
        for (int i = 1; i <= WINDOW_COUNT; i++) {
            new Thread(new TicketWindow(i)).start();
        }
    }

    // 售票窗口类，实现Runnable接口
    static class TicketWindow implements Runnable {
        private final int windowId;

        public TicketWindow(int windowId) {
            this.windowId = windowId;
        }

        @Override
        public void run() {
            while (true) {
                synchronized (TicketSaleSimulation.class) {
                    if (soldTickets >= TOTAL_TICKETS) {
                        break;
                    }
                    soldTickets++;
                    System.out.println("窗口" + windowId + "售出第" + soldTickets + "张票");
                }
                try {
                    // 模拟售票过程中的延迟
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

